// Project: Salient
// http://salient.codeplex.com
// 
// Copyright 2010, Sky Sanders <sky at skysanders.net>
// Dual licensed under the MIT or GPL Version 2 licenses.
// http://salient.codeplex.com/license
// 
// Date: April 25 2010 

#region

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

#endregion

namespace Salient.Reflection
{
    ///<summary>
    ///</summary>
    public static class AttributeHelpers
    {
        /// <summary>
        /// Returns first non-inherited custom attribute of type T
        /// </summary>
        public static T GetCustomAttribute<T>(this ICustomAttributeProvider provider)
            where T : Attribute
        {
            return GetCustomAttribute<T>(provider, false);
        }

        /// <summary>
        /// Returns first custom attribute of type T in the inheritance chain
        /// </summary>
        public static T GetCustomAttribute<T>(this ICustomAttributeProvider provider, bool inherited)
            where T : Attribute
        {
            return provider.GetCustomAttributes<T>(inherited).FirstOrDefault();
        }

        /// <summary>
        /// Returns all non-inherited custom attributes of type T 
        /// </summary>
        public static List<T> GetCustomAttributes<T>(this ICustomAttributeProvider provider)
            where T : Attribute
        {
            return GetCustomAttributes<T>(provider, false);
        }

        /// <summary>
        /// Returns all custom attributes of type T in the inheritance chain
        /// </summary>
        public static List<T> GetCustomAttributes<T>(this ICustomAttributeProvider provider, bool inherited)
            where T : Attribute
        {
            return provider.GetCustomAttributes(typeof (T), inherited).Cast<T>().ToList();
        }
    }
}